import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class FlutterEasyrefreshPage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  FlutterEasyrefreshPage({Key key, this.arguments}) : super(key: key);

  @override
  _FlutterEasyrefreshPageState createState() => _FlutterEasyrefreshPageState();
}

class _FlutterEasyrefreshPageState extends State<FlutterEasyrefreshPage> {
  EasyRefreshController _controller;

  int page = 0;
  List<JokeModel> models = List();
  int _count = 0;

  @override
  void initState() {
    super.initState();
    //
    _controller = EasyRefreshController();

    // // 加载数据
    // fetchModel(page: 1).then((value) {
    //   setState(() {
    //     this.models += value;
    //   });
    // });

    //
    // _controller.callRefresh();
  }

  /// 网络获取数据
  Future<List<JokeModel>> fetchModel({@required int page}) async {
    Map<String, dynamic> params = {
      'page': page,
      'count': 20,
      'type': 'video',
    };
    final response = await Dio().get('https://api.apiopen.top/getJoke', queryParameters: params);

    List result = response.data['result'];
    final jokeModels = result.map((map) => JokeModel.fromJson(map)).toList();

    return jokeModels;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("EasyRefresh"),
      ),
      body: EasyRefresh.custom(
        // firstRefresh: true,
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        controller: _controller,
        header: ClassicalHeader(showInfo: false, completeDuration: Duration(milliseconds: 000)),
        footer: ClassicalFooter(showInfo: false),
        onRefresh: () async {
          final loadPage = 1;
          await this.fetchModel(page: loadPage).then((value) {
            //
            setState(() {
              //
              this.page = loadPage;

              //
              this.models.clear();
              this.models += value;
            });
          });
        },
        onLoad: () async {
          final loadPage = this.page + 1;
          await this.fetchModel(page: loadPage).then((value) {
            //
            setState(() {
              //
              this.page = loadPage;

              //
              this.models += value;
            });
          });
        },
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return Container(
                  width: 60.0,
                  height: 60.0,
                  child: Center(
                    child: Text(this.models[index].text),
                  ),
                  color: index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
                );
              },
              childCount: this.models.length,
            ),
          ),
        ],
      ),
      persistentFooterButtons: <Widget>[
        FlatButton(
            onPressed: () {
              _controller.callRefresh();
            },
            child: Text("Refresh", style: TextStyle(color: Colors.black))),
        FlatButton(
            onPressed: () {
              _controller.callLoad();
            },
            child: Text("Load more", style: TextStyle(color: Colors.black))),
      ],
    );
  }
}

class JokeModel {
  final String text;
  final String thumbnail;
  final String name;
  JokeModel.fromJson(Map<String, dynamic> json)
      : this.text = json['text'],
        this.thumbnail = json['thumbnail'],
        this.name = json['name'];
}

final _json = {
  "sid": "31176707",
  "text": "那你嫁给她哥。",
  "type": "video",
  "thumbnail": "http://wimg.spriteapp.cn/picture/2020/0425/5ea407e8d6f5d_wpd.jpg",
  "video": "http://uvideo.spriteapp.cn/video/2020/0425/5ea407e8d6f5d_wpd.mp4",
  "images": null,
  "up": "116",
  "down": "18",
  "forward": "0",
  "comment": "12",
  "uid": "23132731",
  "name": "熙熙",
  "header": "http://wimg.spriteapp.cn/profile/large/2019/07/04/5d1d9691ae965_mini.jpg",
  "top_comments_content": "不得姐我有点坚守不下去了",
  "top_comments_voiceuri": "",
  "top_comments_uid": "8881182",
  "top_comments_name": "御弟哥哥",
  "top_comments_header": "http://wimg.spriteapp.cn/profile/large/2018/09/23/5ba6d95f3996d_mini.jpg",
  "passtime": "2020-04-28 19:49:02"
};
